package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.ServiceCodeDef;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IServiceCodeDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IServiceCodeDefService;
import com.huitone.gddw.service.ISvrNodeCodeRltService;
import com.huitone.gddw.service.dto.ServiceCodeDefDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
* @Title ServiceCodedefService
* @Package com.huitone.gddw.service.impl
* @Description TODO
* @author chengsiyue
* @date 2017年4月27日 下午3:42:05
* @version V1.0   
*/
@Service
public class ServiceCodeDefService implements IServiceCodeDefService{
	@Autowired
	IServiceCodeDefDao serviceCodeDefDao;
	@Autowired
	private ISvrNodeCodeRltService svrNodeCodeRltService;
	
	@Autowired
	private IParamtabRecService paramtabRecService;

	@Override
	public int delete(String serviceCode) {
		HashMap<String, Object> map = new HashMap<String,Object>();
		map.put("serviceCode",serviceCode);
		svrNodeCodeRltService.deleteByServiceCode(map);
		this.updateParamRec("删除短信类型");
		return serviceCodeDefDao.delete(serviceCode);
	}

	@Override
	public int insert(ServiceCodeDef record) {
		record.setInsertStaffId(SysLoginUtil.getLoginUserId());
		record.setInsertTime(new Date());
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		this.updateParamRec("保存短信类型");
		return serviceCodeDefDao.insert(record);
	}

	@Override
	public int save(String serviceCode) {
		ServiceCodeDef serviceCodeDef =new ServiceCodeDef();
		serviceCodeDef.setInsertStaffId(SysLoginUtil.getLoginUserId());
		serviceCodeDef.setInsertTime(new Date());
		serviceCodeDef.setLastModStaffId(SysLoginUtil.getLoginUserId());
		serviceCodeDef.setLastModTime(new Date());
		serviceCodeDef.setServiceCode(serviceCode);
		this.updateParamRec("根据短信类型编码保存短信类型信息");
		return serviceCodeDefDao.save(serviceCodeDef);
	}

	@Override
	public ServiceCodeDef selectByPrimaryKey(String serviceCode) {
		return serviceCodeDefDao.selectByPrimaryKey(serviceCode);
	}

	@Override
	public int update(ServiceCodeDef record) {
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		this.updateParamRec("更新短信类型");
		return serviceCodeDefDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(ServiceCodeDef record) {
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		this.updateParamRec("更新短信类型");
		return serviceCodeDefDao.updateByPrimaryKey(record);
	}

//	@Override
//	public QueryPage<ServiceCodeDefDTO> selectPage(QueryPage<ServiceCodeDef> qp) {
//		return null;
//	}


	@Override
	public ServiceCodeDefDTO toUpdate(String serviceCode) {
		return new ServiceCodeDefDTO(serviceCodeDefDao.toUpdate(serviceCode));
	}

	@Override
	public QueryPage<ServiceCodeDefDTO> selectPage(QueryPage<ServiceCodeDef> qp) {
		Map<String,Object>param=new HashMap<>();
		param.put("serviceCode", qp.getParams().get("serviceCode"));
		param.put("serviceName", qp.getParams().get("serviceName"));
		param.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
		param.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
		List<ServiceCodeDefDTO> result = serviceCodeDefDao.selectPage(param);
		QueryPage<ServiceCodeDefDTO> queryPage=new QueryPage<>();
		int totalCount=serviceCodeDefDao.selectCount(param);
		queryPage.setResult(result);
		queryPage.setPageNo(qp.getPageNo());
		queryPage.setPageSize(qp.getPageSize());
		queryPage.setTotalCount(totalCount);
		
		return  queryPage;
	}

	@Override
	public List<ServiceCodeDef> getBySvrNode(String svrNode) {
		
		List<ServiceCodeDef> list = serviceCodeDefDao.getBySvrNode(svrNode);
		return list;
	}

	@Override
	public int saveRecord(ServiceCodeDef record) {
		record.setInsertStaffId(SysLoginUtil.getLoginUserId());
		record.setInsertTime(new Date());
		this.updateParamRec("保存短信类型");
		return serviceCodeDefDao.save(record);
	}

	@Override
	public List<ServiceCodeDefDTO> getRecord(Map<String, Object> map) {
		List<ServiceCodeDef> record = serviceCodeDefDao.getRecord(map);
		ServiceCodeDefDTO serviceCodeDefDTO;
		List <ServiceCodeDefDTO>list=new ArrayList<>();
		for(int i=0;i<record.size();i++){
			serviceCodeDefDTO = new ServiceCodeDefDTO(record.get(i));
			serviceCodeDefDTO.setSeq(i+1);
			list.add(serviceCodeDefDTO);
			
		}
		return  list;
	}

	@Override
	public List<ServiceCodeDef> getAll() {
		return serviceCodeDefDao.getAll();
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SERVICE_CODE_DEF, desc);
	}

}
